package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * ---启动页
 */
public class StartActivity extends BaseActivity implements View.OnClickListener {
    private static final int END_JUMP = 10;
    private ImageView iv_start;
    private TextView tv_second;
    private int time = 0;
    private Handler myHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    time--;
                    if (time < 0) {
                        time = 0;
                    }
                    tv_second.setText(time + "s");
                    if (myHandler == null) {
                        return;
                    }
                    myHandler.sendEmptyMessageDelayed(1, 1000);
                    break;

                case END_JUMP:
                    Intent intent = new Intent(StartActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
//                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

                    break;

            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        //
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        iv_start = (ImageView) findViewById(R.id.iv_start);
        tv_second = (TextView) findViewById(R.id.tv_second);
        findViewById(R.id.rl_jump).setOnClickListener(this);
        upload();
    }

    private static final String TAG = "StartActivity";

    private void upload() {
        XUtil.Post(Url.GETADVE, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i(TAG, "onSuccess:GETADVE= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONObject json = jsonObject.optJSONObject("info");
                    String img = json.optString("img");
                    String url = json.optString("url");

                    if ("1".equals(res)) {
                        //   x.image().bind(iv_start, img);
                        ImageLoader.getInstance().displayImage(img, iv_start);
                        time = 2;
                        myHandler.sendEmptyMessage(1);
                        myHandler.sendEmptyMessageDelayed(END_JUMP, 2000);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (myHandler != null) {
            Log.e("---", "--cnm-");
            myHandler.removeCallbacksAndMessages(null);
            //   myHandler.removeMessages(1);
            myHandler = null;
        }
//        RefWatcher refWatcher = MyApplication.getRefWatcher(this);
//        refWatcher.watch(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_jump:
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
                if (myHandler != null) {
                    myHandler.removeMessages(10);
                    myHandler.removeCallbacks(null);
                    myHandler = null;
                }
                finish();
                break;

            default:
                break;
        }
    }
}
